﻿/****************************************************************** 
 * 文件名.........: MenuController.cs 
 * 作者...........: 中文姓名 
 * 说明...........: 用户登录SESSION信息类 
 * 注意...........: 
 * 修改记录.......:   时间       人员    备注
 *                    2011-01-15 XXXX 创建文件，初始设计
 *                    2011-01-16 XXXX 增加安全部分的加密功能
 * ******************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using QzgfFrame.Controllers.CommonSupport;
using QzgfFrame.System.DepartmentManger.Domain;
using QzgfFrame.System.DepartmentManger.Models;
using QzgfFrame.Utility.Core.Common;
using QzgfFrame.Utility.Core.Import;
using System.Web;
using QzgfFrame.Utility.Core.JSON;

namespace QzgfFrame.Controllers.System.DepartmentManger
{
    public class DepartmentController : BaseController
    {
        #region 变量定义

        private DepartmentFacade departmentFacade { set; get; }

        #endregion

        #region 基本操作

        /// <summary>
        /// 首页信息调用grid,时通过service取得数据
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
           var tmp= currentUser.MenuPermission;
            return View();
        }

        //
        // GET: /User/Home/Edit/5
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Add(string id)
        {
            var result = new SystemDepartment();
            result.Id = "0";
            return View("Edit", result);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Modify(string id)
        {
            SystemDepartment result = departmentFacade.Get(id);
            return View("Edit", result);
        }

        //
        // POST: /User/Home/Edit/5
        //[AcceptVerbs(HttpVerbs.Post)]
        //FormCollection
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id, SystemDepartment entity)
        {
            try
            {
                if (id == "0")
                    this.departmentFacade.Save(entity);
                else
                    this.departmentFacade.Update(entity);
                return View();
            }
            catch (Exception e)
            {
                return View();
            }
        }

        //
        // GET: /User/Home/Delete/5,2,1
        //[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(string id)
        {
            bool result = false;
            string msg = "操作失败";
            result = departmentFacade.Delete(id.ToString());
            if (result) msg = "操作成功";
            return Json(new {Type = result, Message = msg}, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 默认主页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <param name="sortname"></param>
        /// <param name="sortorder"></param>
        /// <param name="gridviewname"></param>
        /// <param name="gridsearch"></param>
        /// <returns></returns>
        public override string GridPager(int page, int pagesize, string sortname, string sortorder, string gridviewname,
                                         string gridsearch)
        {
            return departmentFacade.FindByPage(page, pagesize, sortname, sortorder);
        }


        #endregion

        #region 加强

        //扩展页面
        public ActionResult IndexPermissions()
        {
            return View();
        }

        /// <summary>
        /// 取得父结点信息
        /// </summary>
        /// <returns></returns>
        public string GetFather()
        {
            return departmentFacade.GetFather();
        }
        /// <summary>
        /// 修改用户状态
        /// </summary>
        /// <param name="id">字符串数组</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        public ActionResult UseStart(string id, string state)
        {
            bool result = false;
            string msg = "操作失败";

            result = departmentFacade.UseState(id, state);
            if (result) msg = "操作成功";
            return Json(new {Type = result, Message = msg}, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 修改用户状态
        /// </summary>
        /// <param name="id">字符串数组</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        public ActionResult UseStop(string id, string state)
        {
            bool result = false;
            string msg = "操作失败";

            result = departmentFacade.UseState(id, state);
            if (result) msg = "操作成功";
            return Json(new { Type = result, Message = msg }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 数据导出
        /// </summary>
        /// <returns></returns>
        public ExcelResult<SystemDepartment> ExportExcel()
        {
            //方案一
            //var ex = new ExportUtil<SystemDepartment>();
            //Hashtable list = ex.ExportConfig("~/Config/exports.xml");
            //var exportInfo = list["exportMenu"] as ExportInfo;
            //var outdata = new byte[] {};
            //ex.Exportlist = departmentFacade.LoadAll();
            //if (exportInfo != null)
            //    ex.Export(out outdata,"xls",exportInfo.GetFileName(),exportInfo.GetFields(),exportInfo.GetColumns());
            ////var ret = new FileContentResult(outdata, "application/vnd.ms-excel");
            //Response.AddHeader("Content-Disposition ", String.Format("attachment;Filename= 导出{0}.xls ", "部门信息"));
            ////Response.AddHeader("Content-Disposition", "inline; filename=test.pdf"); 
            //var ret = File(outdata, "application/vnd.ms-excel","部门信息.xls"); 
            ////return File(outdata, "application/vnd.ms-excel", String.Format("导出{0}.xls", "部门信息"));
            //return ret;
            IList<SystemDepartment> ls = departmentFacade.LoadAll();
            return new ExcelResult<SystemDepartment>(ls, "测试", "exportMenu");
        }

        //导入excel数据
        public ActionResult ImportView()
        {
            return View("ImportExcel");
        }

        public ActionResult ImportExcel()
        {
            string msg = "";
            try
            {
                //上传
                HttpPostedFileBase file = Request.Files[0];
                ArrayList msgarray = new ImportUtil().Import(file, "xls", "importMenu", "/importexcel/");
                msg = JSONHelper.ToJSON(msgarray);
                //导入
                //String[] tmp = { "菜单ID", "菜单名称" };
                //String[] tmp1 = { "id", "name" };
                //var er = new ImportFromPoiExcel("E:/desktop/测试.xls", tmp, tmp1);
                //Hashtable line = er.readLine();
                //while (line != null)
                //{
                //    Console.WriteLine(line);
                //    line = er.readLine();
                //}
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return Json(new { Message = msg }, JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}
